package com.huizhonglm.diap.core.storage.pref;

import android.content.Context;
import android.content.SharedPreferences;

import com.huizhonglm.diap.DiapApplication;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2015-12-22 10:49
 */
public class Preferences {
    private static final String NAME = "diapClientPreferences";

    private static Preferences mInstance;

    private SharedPreferences pref = null;

    /**
     * 获取单例
     *
     * @return Preferences
     */
    public static Preferences getInstance() {
        if (mInstance == null) {
            synchronized (Preferences.class) {
                if (mInstance == null) {
                    mInstance = new Preferences();
                }
            }
        }
        return mInstance;
    }

    private Preferences() {
        pref = DiapApplication.getInstance().getSharedPreferences(NAME, Context.MODE_PRIVATE);
    }

    /**
     * 存储字符串的信息
     *
     * @param key   The name of the preference to modify.
     * @param value The new value for the preference.
     */
    public void putString(String key, String value) {
        pref.edit().putString(key, value).apply();
    }

    /**
     * 获取字符串的信息
     *
     * @param key      The name of the preference to retrieve.
     * @param defValue Value to return if this preference does not exist.
     * @return Returns the preference value if it exists, or defValue.
     */
    public String getString(String key, String defValue) {
        return pref.getString(key, defValue);
    }

    /**
     * 存储布尔值的信息
     *
     * @param key   The name of the preference to modify.
     * @param value The new value for the preference.
     */
    public void putBoolean(String key, boolean value) {
        pref.edit().putBoolean(key, value).apply();
    }

    /**
     * 获取布尔值的信息
     *
     * @param key      The name of the preference to retrieve.
     * @param defValue Value to return if this preference does not exist.
     * @return Returns the preference value if it exists, or defValue.
     */
    public boolean getBoolean(String key, boolean defValue) {
        return pref.getBoolean(key, defValue);
    }

    /**
     * 存储浮点型数值的信息
     *
     * @param key   The name of the preference to modify.
     * @param value The new value for the preference.
     */
    public void putFloat(String key, Float value) {
        pref.edit().putFloat(key, value).apply();
    }

    /**
     * 获取浮点型数值的信息
     *
     * @param key      The name of the preference to retrieve.
     * @param defValue Value to return if this preference does not exist.
     * @return Returns the preference value if it exists, or defValue.
     */
    public Float getFloat(String key, Float defValue) {
        return pref.getFloat(key, defValue);
    }

    /**
     * 存储长整型数值的信息
     *
     * @param key   The name of the preference to modify.
     * @param value The new value for the preference.
     */
    public void putLong(String key, Long value) {
        pref.edit().putLong(key, value).apply();
    }

    /**
     * 获取长整型数值的信息
     *
     * @param key      The name of the preference to retrieve.
     * @param defValue Value to return if this preference does not exist.
     * @return Returns the preference value if it exists, or defValue.
     */
    public Long getLong(String key, long defValue) {
        return pref.getLong(key, defValue);
    }

    /**
     * 存储整型数值的信息
     *
     * @param key   The name of the preference to modify.
     * @param value The new value for the preference.
     */
    public void putInt(String key, int value) {
        pref.edit().putInt(key, value).apply();
    }

    /**
     * 获取整型数值的信息
     *
     * @param key      The name of the preference to retrieve.
     * @param defValue Value to return if this preference does not exist.
     * @return Returns the preference value if it exists, or defValue.
     */
    public int getInt(String key, int defValue) {
        return pref.getInt(key, defValue);
    }

    public SharedPreferences.Editor edit(){
        return pref.edit();
    }

    public void apply(){
        pref.edit().apply();
    }
}
